<?php


/**
 * JXHEditor class file.
 *
 * @author jerry2801 <jerry2801@gmail.com>
 *
 * A typical usage of JXHEditor is as follows:
 * <pre>
 * $this->widget('application.extensions.xheditor.JXHEditor', array(
 *     'model' => $model,
 *     'attribute' => 'intro',
 *     'options' => array(
 *         'width' => '100%',
 *         'height' => 400,
 *     ),
 * ));
 * </pre>
 */

class JXHEditor extends CWidget {
	public $model;
	public $attribute;
	public $baseUrl;
	public $language = 'en';
	public $options = array ();
	public $htmlOptions = array ();
	public $id = null;
	public $register_only = false;

	public function init() {
		if ($this->id != null) {
			$activeId = $this->id;
		} else
			if (!$this->register_only) {
				$activeId = CHtml::activeId($this->model, $this->attribute);
			}
		$options = CJavaScript::jsonEncode($this->options);

		$dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'source';
		$this->baseUrl = Yii::app()->getAssetManager()->publish($dir);

		$cs = Yii::app()->getClientScript();
		$cs->registerScriptFile($this->baseUrl . '/xheditor-' . $this->language . '.js');

		if (!$this->register_only)
			$cs->registerScript($activeId .
			'-Script', '$("#' . $activeId . '").xheditor(true,' . $options . ');');

	}

	public function run() {
		if ($this->id == null && !$this->register_only)
			echo CHtml::activeTextArea($this->model, $this->attribute, $this->htmlOptions);
	}
}
?>